/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package eypweb_bsc;

import java.net.URL;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;

/**
 *
 * @author Kohalmi Istvan
 */

// vagoynkezelo valaszto kepernyot megvalosito osztaly
public class TrusteeScreen implements Initializable {
    // vagyonkezelok listaja
    ArrayList<Trustee> list;
    // vagyonkezelo valaszto vezerlo
    @FXML
    private ChoiceBox cbTrustee;
    // cimke uzenetekhez
    @FXML
    private Label errorLabel;
    
    public TrusteeScreen()
    {
        list = new ArrayList<Trustee>();
    }
    
    @FXML
    private void handleSelectButtonAction(ActionEvent event) {
        Director director = Director.getInstance();
        Trustee t= new Trustee();
        String name = cbTrustee.getSelectionModel().getSelectedItem().toString();
        for (Trustee tr: list)
        {
            if (tr.getName().equals(name))
            {
                t=tr;
                break;
            }
        }
        director.selectTrustee(t);
        director.initializeOperationScreen();
        cbTrustee.getScene().getWindow().hide();
    }
    
    @FXML
    private void handleCancelButtonAction(ActionEvent event) {
        cbTrustee.getScene().getWindow().hide();
    }
    
    @FXML
    private void handleRefreshButtonAction(ActionEvent event){
        refresh();
    }
    
    private void refresh()
    {
        list.clear();
        cbTrustee.getItems().clear();
        Director director = Director.getInstance();
        AccessInfo ai = director.getAccessInfo();
        try{
            Connection con = DriverManager.getConnection("jdbc:sqlserver://" + ai.getIPAddress() + ":" + ai.getPort() + ";databaseName=" + ai.getDBName(), ai.getUser(), ai.getPass());
            String query = "SELECT id, id_vk, nev, r_nev FROM vagyonkezelo WHERE ? BETWEEN hatalytol AND hatalyig";
            PreparedStatement stmt = con.prepareStatement(query);
            Calendar now = Calendar.getInstance();
            SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
            String dateNow = sdf.format(now.getTime());
            stmt.setDate(1, Date.valueOf(dateNow));
            ResultSet rs = stmt.executeQuery();
            while(rs.next())
            {
                Trustee trst = new Trustee();
                trst.setId(rs.getInt(1));
                trst.setIdTrustee(rs.getInt(2));
                trst.setName(rs.getNString(3));
                trst.setShortName(rs.getNString(4));
                list.add(trst);
                cbTrustee.getItems().add(trst.getName());
            }
        }
        catch(SQLException sqle)
        {
            errorLabel.setText(sqle.getMessage());
        }
        cbTrustee.getSelectionModel().select(0);
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        refresh();
        
    }    
}
